home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / inettime / inettime.c next >
C/C++ Source or Header  |  1989-12-17  |  3KB  |  143 lines

  1. #include    "systype.h"    /* for index() function */
  2. #include    <sys/types.h>
  3. #include    <sys/socket.h>
  4. #include    <stdio.h>
  5. #include    <sys/errno.h>
  6. extern int    errno;
  7.  
  8. #define    MAXLINE          512    /* max line length */
  9. #define    TVAL_SIZE        4    /* 4 bytes in the 32-bit binary timeval */
  10.  
  11. /*
  12.  * Contact the time server using TCP/IP and print the result.
  13.  */
  14.  
  15. tcp_time(host)
  16. char    *host;
  17. {
  18.     int        fd, i;
  19.     unsigned long    temptime, timeval;
  20.  
  21.     /*
  22.      * Initiate the connection and receive the 32-bit time value
  23.      * (in network byte order) from the server.
  24.      */
  25.  
  26.     if ( (fd = tcp_open(host, "time", 0)) < 0) {
  27.         err_ret("tcp_open error");
  28.         return;
  29.     }
  30.  
  31.     if ( (i = readn(fd, (char *) &temptime, TVAL_SIZE)) != TVAL_SIZE)
  32.         err_dump("received %d bytes from server", i);
  33.  
  34.     timeval = ntohl(temptime);
  35.     printf("time from host %s using TCP/IP = %lu\n", host, timeval);
  36.  
  37.     close(fd);
  38. }
  39.  
  40. /*
  41.  * Contact the daytime server using TCP/IP and print the result.
  42.  */
  43.  
  44. tcp_daytime(host)
  45. char    *host;
  46. {
  47.     int    fd;
  48.     char    *ptr, buff[MAXLINE], *index();
  49.  
  50.     /*
  51.      * Initiate the session and receive the netascii daytime value
  52.      * from the server.
  53.      */
  54.  
  55.     if ( (fd = tcp_open(host, "daytime", 0)) < 0) {
  56.         err_ret("tcp_open error");
  57.         return;
  58.     }
  59.  
  60.     if(readline(fd, buff, MAXLINE) < 0)
  61.         err_dump("readline error");
  62.  
  63.     if ( (ptr = index(buff, '\n')) != NULL)
  64.         *ptr = 0;
  65.     printf("daytime from host %s using TCP/IP = %s\n", host, buff);
  66.  
  67.     close(fd);
  68. }
  69.  
  70. /*
  71.  * Contact the time server using UDP/IP and print the result.
  72.  */
  73.  
  74. udp_time(host)
  75. char    *host;
  76. {
  77.     int        fd, i;
  78.     unsigned long    temptime, timeval;
  79.  
  80.     /*
  81.      * Open the socket and send an empty datagram to the server.
  82.      */
  83.  
  84.     if ( (fd = udp_open(host, "time", 0, 0)) < 0) {
  85.         err_ret("udp_open error");
  86.         return;
  87.     }
  88.  
  89.     /*
  90.      * Send a datagram, and read a response.
  91.      */
  92.  
  93.     if ( (i = dgsendrecv(fd, (char *) &temptime, 1, (char *) &temptime,
  94.             TVAL_SIZE, (struct sockaddr *) 0, 0)) != TVAL_SIZE)
  95.         if (errno == EINTR) {
  96.             err_ret("udp_time: no response from server");
  97.             close(fd);
  98.             return;
  99.         } else
  100.             err_dump("received %d bytes from server", i);
  101.  
  102.     timeval = ntohl(temptime);
  103.     printf("time from host %s using UDP/IP = %lu\n", host, timeval);
  104.  
  105.     close(fd);
  106. }
  107.  
  108. /*
  109.  * Contact the daytime server using UDP/IP and print the result.
  110.  */
  111.  
  112. udp_daytime(host)
  113. char    *host;
  114. {
  115.     int    fd, i;
  116.     char    *ptr, buff[MAXLINE], *index();
  117.  
  118.     /*
  119.      * Open the socket and send an empty datagram to the server.
  120.      */
  121.  
  122.     if ( (fd = udp_open(host, "daytime", 0, 0)) < 0) {
  123.         err_ret("udp_open error");
  124.         return;
  125.     }
  126.  
  127.     if ( (i = dgsendrecv(fd, buff, 1, buff, MAXLINE,
  128.                 (struct sockaddr *) 0, 0)) < 0)
  129.         if (errno == EINTR) {
  130.             err_ret("udp_daytime: no response from server");
  131.             close(fd);
  132.             return;
  133.         } else
  134.             err_dump("read error");
  135.  
  136.     buff[i] = 0;        /* assure its null terminated */
  137.     if ( (ptr = index(buff, '\n')) != NULL)
  138.         *ptr = 0;
  139.     printf("daytime from host %s using UDP/IP = %s\n", host, buff);
  140.  
  141.     close(fd);
  142. }
  143.